package com.lc.pianostudent.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.GridAdapter;
import com.lc.pianostudent.adapter.PianoRecordAdapter;
import com.lc.pianostudent.adapter.XingzuoAdapter;
import com.lc.pianostudent.conn.PostConstellationIndex;
import com.lc.pianostudent.conn.PostGetPintu;
import com.lc.pianostudent.conn.PostGethg;
import com.lc.pianostudent.conn.PostMailXingka;
import com.lc.pianostudent.conn.PostrResetPasswordNext;
import com.lc.pianostudent.dialog.ShopDialog;
import com.lc.pianostudent.dialog.XingzuoDialog;
import com.lc.pianostudent.fragment.MineFragment;
import com.lc.pianostudent.util.Util;
import com.lc.pianostudent.view.SelectPopupWindow;
import com.lc.pianostudent.widget.GridDividerItemDecoration;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/26.
 */

public class ConstellationActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.conversion_record_layout,isClick = true) private LinearLayout conversion_record_layout;
    @BoundView(R.id.xing_count) private TextView xing_count;
    @BoundView(R.id.xingzuo_content) private TextView xingzuo_content;
    @BoundView(R.id.xingzuo_count) private TextView xingzuo_count;

    @BoundView(R.id.huangguan1) private ImageView huangguan1;
    @BoundView(R.id.huangguan2) private ImageView huangguan2;
    @BoundView(R.id.huangguan3) private ImageView huangguan3;
    @BoundView(R.id.huangguan4) private ImageView huangguan4;
    @BoundView(R.id.huangguan5) private ImageView huangguan5;
    @BoundView(value = R.id.xingzuo_img,isClick = true) private ImageView xingzuo_img;

    @BoundView(R.id.image_recycle_view) private RecyclerView image_recycle_view;
    private List<String> imgList = new ArrayList<>();  //上传记录集合
    private XingzuoAdapter xingzuoAdapter;
    private PostConstellationIndex.PostConstellationIndexInfo currentInfo;
    private int width = 720;
    private int height = 1280;
    private SelectPopupWindow menuWindow;
    private String num = "";

    @BoundView(value = R.id.get_pintu,isClick = true) private TextView get_pintu;
    @BoundView(value = R.id.get_xingguan,isClick = true) private TextView get_xingguan;
    //星卡点击看大图
    public PostMailXingka postMailXingka = new PostMailXingka(new AsyCallBack<PostMailXingka.PostMailXingkaInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostMailXingka.PostMailXingkaInfo postMailXingkaInfo) throws Exception {
            menuWindow = new SelectPopupWindow(ConstellationActivity.this,postMailXingkaInfo.imgList,0);
            //显示窗口 //设置layout在PopupWindow中显示的位置
            menuWindow.showAtLocation(xing_count, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    //兑换拼图
    private PostGetPintu postGetPintu = new PostGetPintu(new AsyCallBack<PostGetPintu.PostGetPintuInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostGetPintu.PostGetPintuInfo postGetPintuInfo) throws Exception {
           if(postGetPintuInfo.type.equals("true")){
//               兑换整卡
               new XingzuoDialog(ConstellationActivity.this, R.layout.dialog_xingzuo,postGetPintuInfo.picurl) {

               }.show();
               UtilToast.show(postGetPintuInfo.text);
           }else {
//               不是兑换证卡
               UtilToast.show(toast);

           }
            try {
                ((ConstellationActivity.CallBack)getAppCallBack(ConstellationActivity.class)).onRefresh();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });

    //兑换皇冠
    private PostGethg postGethg = new PostGethg(new AsyCallBack<PostGethg.PostGethgInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostGethg.PostGethgInfo postGethgInfo) throws Exception {

            UtilToast.show(toast);

            try {
                ((ConstellationActivity.CallBack)getAppCallBack(ConstellationActivity.class)).onRefresh();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });

    //星座馆首页
    private PostConstellationIndex postConstellationIndex = new PostConstellationIndex(new AsyCallBack<PostConstellationIndex.PostConstellationIndexInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostConstellationIndex.PostConstellationIndexInfo postConstellationIndexInfo) throws Exception {
            xing_count.setText("x"+postConstellationIndexInfo.xx);
            xingzuo_content.setText(postConstellationIndexInfo.title+":"+postConstellationIndexInfo.content);
            xingzuo_count.setText("x"+postConstellationIndexInfo.num);
            currentInfo = postConstellationIndexInfo;
            num = postConstellationIndexInfo.num;

            if(Integer.parseInt(postConstellationIndexInfo.xx) <10){
                get_pintu.setBackgroundResource(R.mipmap.star_button2);
                get_pintu.setTextColor(Color.parseColor("#ffffff"));
            }else {
                get_pintu.setBackgroundResource(R.mipmap.star_button1);
                get_pintu.setTextColor(Color.parseColor("#ffffff"));
            }

            if(Integer.parseInt(postConstellationIndexInfo.num) <12){
                get_xingguan.setBackgroundResource(R.mipmap.star_button2);
                get_xingguan.setTextColor(Color.parseColor("#ffffff"));
            }else {
                get_xingguan.setBackgroundResource(R.mipmap.star_button1);
                get_xingguan.setTextColor(Color.parseColor("#ffffff"));
            }
            //皇冠
            if(postConstellationIndexInfo.huangguan.equals("0")){
                huangguan1.setVisibility(View.INVISIBLE);
                huangguan2.setVisibility(View.INVISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
                huangguan4.setVisibility(View.INVISIBLE);
                huangguan5.setVisibility(View.INVISIBLE);
            }else if(postConstellationIndexInfo.huangguan.equals("1")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.INVISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
                huangguan4.setVisibility(View.INVISIBLE);
                huangguan5.setVisibility(View.INVISIBLE);
            }else if(postConstellationIndexInfo.huangguan.equals("2")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
                huangguan4.setVisibility(View.INVISIBLE);
                huangguan5.setVisibility(View.INVISIBLE);
            }else if(postConstellationIndexInfo.huangguan.equals("3")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.VISIBLE);
                huangguan4.setVisibility(View.INVISIBLE);
                huangguan5.setVisibility(View.INVISIBLE);
            }else if(postConstellationIndexInfo.huangguan.equals("4")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.VISIBLE);
                huangguan4.setVisibility(View.VISIBLE);
                huangguan5.setVisibility(View.INVISIBLE);
            }else if(postConstellationIndexInfo.huangguan.equals("5")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.VISIBLE);
                huangguan4.setVisibility(View.VISIBLE);
                huangguan5.setVisibility(View.VISIBLE);
            }


            if(postConstellationIndexInfo.number.equals("4")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,2));
            }else if(postConstellationIndexInfo.number.equals("6")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,2));
            }else if(postConstellationIndexInfo.number.equals("9")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,3));
            }else if(postConstellationIndexInfo.number.equals("12")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,3));
            }else if(postConstellationIndexInfo.number.equals("16")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,4));
            }else if(postConstellationIndexInfo.number.equals("20")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,4));
            }else if(postConstellationIndexInfo.number.equals("25")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,5));
            }else if(postConstellationIndexInfo.number.equals("30")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,5));
            }else if(postConstellationIndexInfo.number.equals("36")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,6));
            }else if(postConstellationIndexInfo.number.equals("42")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,6));
            }else if(postConstellationIndexInfo.number.equals("49")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,7));
            }else if(postConstellationIndexInfo.number.equals("56")){
                image_recycle_view.setLayoutManager(new GridLayoutManager(ConstellationActivity.this,7));
            }


            xingzuoAdapter = new XingzuoAdapter(ConstellationActivity.this, imgList,currentInfo.number,width);
            image_recycle_view.setAdapter(xingzuoAdapter);


            imgList.addAll(postConstellationIndexInfo.imageList);
            xingzuoAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constellation);
        setBackTrue();
        setAppCallBack(new CallBack());
        initView();
    }

    private void initView(){
        setTitleName("星座馆");
        image_recycle_view.addItemDecoration(new GridDividerItemDecoration(1, Color.parseColor("#000000")));
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）
        height = dm.heightPixels;



        postConstellationIndex.user_id = BaseApplication.BasePreferences.readUID();
        postConstellationIndex.execute();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.conversion_record_layout:
                startVerifyActivity(ConversionRecordActivity.class);
                break;

            case R.id.get_pintu:
                if(Integer.parseInt(currentInfo.xx) <10){
                    UtilToast.show("您当前的星星数量小于10颗，不可以兑换拼图哦~");
                }else {
                    postGetPintu.user_id = BaseApplication.BasePreferences.readUID();
                    postGetPintu.num = currentInfo.num;
                    postGetPintu.number = currentInfo.number;
                    postGetPintu.pintuid = currentInfo.pintuid;
                    postGetPintu.xx = currentInfo.xx;
                    postGetPintu.pintunum = currentInfo.pintunum;
                    postGetPintu.execute();
                }

                break;

            case R.id.get_xingguan:

                if(Integer.parseInt(currentInfo.num) <12){
                    UtilToast.show("您当前的星座数量小于12，不可以兑换星冠哦~");
                }else {
                    postGethg.user_id = BaseApplication.BasePreferences.readUID();
                    postGethg.num = currentInfo.num;
                    postGethg.huangguan = currentInfo.huangguan;
                    postGethg.execute();
                }
                break;

            case R.id.xingzuo_img:

                if(num.equals("0")){
                    UtilToast.show("您当前没有星卡~");
                }else {
                    postMailXingka.num = num;
                    postMailXingka.execute(ConstellationActivity.this,false);
                }

                break;
        }
    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {
            imgList.clear();
            postConstellationIndex.user_id = BaseApplication.BasePreferences.readUID();
            postConstellationIndex.execute();

        }

    }
}
